package logic

import (
	"context"
	"errors"
	"video-hub/models/mysql"
	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type CollectAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectAddLogic {
	return &CollectAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectAddLogic) CollectAdd(in *videorpc.CollectAddRequest) (*videorpc.CollectAddResponse, error) {
	// todo: add your logic here and delete this line
	var collect mysql.Collection

	if err := collect.FindId(mysql.DB, in.UserId, in.VideoId); err == nil {
		return nil, errors.New("已收藏该视频,无需重复操作")
	}
	collect = mysql.Collection{
		Model:         gorm.Model{},
		VideoId:       in.VideoId,
		UserId:        in.UserId,
		CollectStatus: in.CollectStatus,
	}
	if err := collect.Add(mysql.DB); err == nil {
		return nil, errors.New("收藏成功")
	}

	return &videorpc.CollectAddResponse{
		CollectionId: int64(collect.ID),
	}, nil
}
